//
// Source code recreated from a .class file by IntelliJ IDEA
// (powered by FernFlower decompiler)
//

package com.lanf.generator.act;

import java.net.InetAddress;
import java.net.NetworkInterface;
import java.util.Base64;
import javax.crypto.Cipher;
import javax.crypto.SecretKey;
import javax.crypto.SecretKeyFactory;
import javax.crypto.spec.DESKeySpec;

public class ActivationCodeGenerator {
    private static final String SECRET_KEY = "IKKUIUJJ878667LFCYEEDDFE";

    public ActivationCodeGenerator() {
    }

    public static boolean isActivationCodeValid(String activationCode) {
        long expirationTime = decryptTimestamp(activationCode);
        return System.currentTimeMillis() <= expirationTime;
    }

    private static long decryptTimestamp(String encryptedTimestamp) {
        try {
            DESKeySpec keySpec = new DESKeySpec("IKKUIUJJ878667LFCYEEDDFE".getBytes());
            SecretKeyFactory keyFactory = SecretKeyFactory.getInstance("DES");
            SecretKey secretKey = keyFactory.generateSecret(keySpec);
            Cipher cipher = Cipher.getInstance("DES");
            cipher.init(2, secretKey);
            byte[] decryptedData = cipher.doFinal(Base64.getDecoder().decode(encryptedTimestamp));
            String timestampStr = new String(decryptedData);
            return Long.parseLong(timestampStr);
        } catch (Exception var7) {
            var7.printStackTrace();
            throw new RuntimeException("激活码无效");
        }
    }

    public static String generateEncryptionResult(String activationCode) {
        String dataToEncrypt = activationCode + get();

        try {
            DESKeySpec keySpec = new DESKeySpec("IKKUIUJJ878667LFCYEEDDFE".getBytes());
            SecretKeyFactory keyFactory = SecretKeyFactory.getInstance("DES");
            SecretKey secretKey = keyFactory.generateSecret(keySpec);
            Cipher cipher = Cipher.getInstance("DES");
            cipher.init(1, secretKey);
            byte[] encryptedData = cipher.doFinal(dataToEncrypt.getBytes());
            return Base64.getEncoder().encodeToString(encryptedData);
        } catch (Exception var7) {
            var7.printStackTrace();
            return null;
        }
    }

    public static boolean isEncryptionResultValid(String encryptionResult) {
        String decryptedData = decryptEncryptionResult(encryptionResult);
        return decryptedData != null && decryptedData.contains(get());
    }

    private static String decryptEncryptionResult(String encryptionResult) {
        try {
            DESKeySpec keySpec = new DESKeySpec("IKKUIUJJ878667LFCYEEDDFE".getBytes());
            SecretKeyFactory keyFactory = SecretKeyFactory.getInstance("DES");
            SecretKey secretKey = keyFactory.generateSecret(keySpec);
            Cipher cipher = Cipher.getInstance("DES");
            cipher.init(2, secretKey);
            byte[] decryptedData = cipher.doFinal(Base64.getDecoder().decode(encryptionResult));
            return new String(decryptedData);
        } catch (Exception var6) {
            var6.printStackTrace();
            return null;
        }
    }

    private static String getMacAddress() {
        try {
            InetAddress localhost = InetAddress.getLocalHost();
            NetworkInterface networkInterface = NetworkInterface.getByInetAddress(localhost);
            byte[] macAddressBytes = networkInterface.getHardwareAddress();
            StringBuilder macAddressBuilder = new StringBuilder();
            byte[] var4 = macAddressBytes;
            int var5 = macAddressBytes.length;

            for(int var6 = 0; var6 < var5; ++var6) {
                byte b = var4[var6];
                macAddressBuilder.append(String.format("%02X:", b));
            }

            String macAddress = macAddressBuilder.toString();
            macAddress = macAddress.substring(0, macAddress.length() - 1);
            return macAddress;
        } catch (Exception var8) {
            var8.printStackTrace();
            return null;
        }
    }

    public static String get() {
        return getMacAddress();
    }
}
